
import { setToken, getToken, getUser, setUser } from '@/utils/auth'
// import axios from 'axios'
// import { Message } from 'element-ui'
import { login, workspace } from '@/api/user'

const state = {
  token: getToken() || '',
  user: getUser() || '',
  data: null
}

const mutations = {
  SET_DATA(state, data) {
    state.data = data
  },

  setToken(state, newToken) {
    state.token = newToken
    setToken(newToken)
  },
  setUser(state, newUser) {
    state.user = newUser
    setUser(newUser)
  }
}
const actions = {
  fetchData({ commit }) {
    return workspace()
      .then(response => {
        commit('SET_DATA', response.data)
        console.log(response)
      })
      .catch(error => {
        console.error(error)
      })
  },
  logout(context) {
    context.commit('getUser', '')
    context.commit('setToken', '')
  },
  async login(context, payload) {
    const res = await login(payload)
    // console.log(res.data.msg)
    // Message.error(res.data.msg)
    const token = res.data.data.token.token
    context.commit('setToken', token)
    context.commit('setUser', res.data.data.user)
  }
  // 测试试用

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
